//
//  RootViewController.m
//  FinanceBBS
//
//  Created by mibo02 on 2017/12/15.
//  Copyright © 2017年 mibo02. All rights reserved.
//

#import "RootViewController.h"
@interface RootViewController ()
@property (nonatomic,strong) UIButton* noDataView;

@end

@implementation RootViewController

- (UIStatusBarStyle)preferredStatusBarStyle{
    return _StatusBarStyle;
}
//动态更新状态栏颜色
-(void)setStatusBarStyle:(UIStatusBarStyle)StatusBarStyle{
    _StatusBarStyle=StatusBarStyle;
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 13.0, *)) {
//        self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    } else {
        // Fallback on earlier versions
    }
    self.view.backgroundColor = [UIColor whiteColor];
    self.navBarTintColor = kNavTintColor;
    self.StatusBarStyle = UIStatusBarStyleDefault;
    self.isShowLeftBack = YES;
    [self.view addSubview:self.customNavigationBar];
}
///改变导航栏y返回按钮
- (void)setLeftBackImg:(NSString *)leftBackImg{
    _leftBackImg = leftBackImg;
    self.isShowLeftBack = YES;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:self.navigationBarHidden animated:NO];
    [self.navigationController.navigationBar setTintColor:self.navBarTintColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName :self.navBarTintColor}];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[[[UIApplication sharedApplication]windows] objectAtIndex:0] endEditing:YES];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

- (void)showLoadingAnimation
{
    [MBProgressHUD showLoadTitile:nil ToView:self.view];
}

- (void)stopLoadingAnimation
{
    [MBProgressHUD hideHUDForView:self.view];
}


- (void)addBackImageView:(NSString *)imageName{
    UIImageView *bacV = [UIImageView new];
    bacV.image = IMAGE_NAMED(imageName);
    bacV.alpha=0.3;
    [self.view addSubview:bacV];
    [bacV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self.view);
    }];
    [self.view sendSubviewToBack:bacV];
}

-(void)showNoDataImage:(NSString *)imageName click:(void(^)(void))clickBlock;
{
    if (!self.noDataView) {
        self.noDataView=[UIButton buttonWithType:UIButtonTypeCustom];
        self.noDataView.imageView.contentMode = UIViewContentModeScaleAspectFill;
        self.noDataView.backgroundColor=[UIColor clearColor];
    }
    [self.noDataView setImage:IMAGE_NAMED(imageName) forState:UIControlStateNormal];
//    [self.noDataView addActionHandler:^(NSInteger tag) {
//        !clickBlock?:clickBlock();
//    }];
    
    
//    kWeakSelf(self);
//    [self.view.subviews enumerateObjectsUsingBlock:^(UITableView* obj, NSUInteger idx, BOOL *stop) {
//        if ([obj isKindOfClass:[UITableView class]]) {
//            if (![obj.subviews containsObject:weakself.noDataView]) {
//                [weakself.noDataView setFrame:CGRectMake(0, 0,obj.frame.size.width, obj.frame.size.height)];
//                [obj addSubview:weakself.noDataView];
//            }
//        }
//    }];
    [self.view addSubview:self.noDataView];
    [self.noDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(AdaptedWidth(60));
        make.right.mas_equalTo(AdaptedWidth(-60));
        make.height.equalTo(self.noDataView.mas_width).multipliedBy(0.66);
    }];
    [self.view bringSubviewToFront:self.noDataView];
}

-(void)removeNoDataImage{
    if (_noDataView) {
        [_noDataView removeFromSuperview];
        _noDataView = nil;
    }
}

/**
 *  是否显示返回按钮
 */
- (void)setIsShowLeftBack:(BOOL)isShowLeftBack
{
    _isShowLeftBack = isShowLeftBack;
    NSInteger VCCount = self.navigationController.viewControllers.count;
    //下面判断的意义是 当VC所在的导航控制器中的VC个数大于1 或者 是present出来的VC时，才展示返回按钮，其他情况不展示
    if (isShowLeftBack && ( VCCount > 1 || self.navigationController.presentingViewController != nil)) {
        [self addNavigationItemWithImageNames:@[self.leftBackImg?:@"black_nav_back"] isLeft:YES target:self action:@selector(backBtnClicked) tags:nil];
    } else {
        self.navigationItem.hidesBackButton = YES;
        UIBarButtonItem * NULLBar=[[UIBarButtonItem alloc]initWithCustomView:[UIView new]];
        self.navigationItem.leftBarButtonItems = @[NULLBar];
    }
}
- (void)backBtnClicked
{
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}


#pragma mark ————— 导航栏 添加图片按钮 —————
/**
 导航栏添加图标按钮
 
 @param imageNames 图标数组
 @param isLeft 是否是左边 非左即右
 @param target 目标
 @param action 点击方法
 @param tags tags数组 回调区分用
 */
- (void)addNavigationItemWithImageNames:(NSArray *)imageNames isLeft:(BOOL)isLeft target:(id)target action:(SEL)action tags:(NSArray *)tags
{
    NSMutableArray * items = [[NSMutableArray alloc] init];
    //调整按钮位置
    UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //将宽度设为负值
    spaceItem.width= -5;
    [items addObject:spaceItem];
    NSInteger i = 0;
    for (NSString * imageName in imageNames) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        btn.frame = CGRectMake(0, 0, 35, 35);
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

        if (isLeft) {
            [btn setContentEdgeInsets:UIEdgeInsetsMake(0, -25, 0, 0)];
        }else{
//            [btn setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, -10)];
        }

        btn.tag = [tags[i++] integerValue];
        UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
        [items addObject:item];

    }
    if (isLeft) {
        self.navigationItem.leftBarButtonItems = items;
    } else {
        self.navigationItem.rightBarButtonItems = items;
    }
}

#pragma mark ————— 导航栏 添加文字按钮 —————
- (void)addNavigationItemWithTitles:(NSArray *)titles isLeft:(BOOL)isLeft target:(id)target action:(SEL)action tags:(NSArray *)tags
{
    
    NSMutableArray * items = [[NSMutableArray alloc] init];
    
    //调整按钮位置
        UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        //将宽度设为负值
        spaceItem.width= -5;
        [items addObject:spaceItem];
    
    NSMutableArray * buttonArray = [NSMutableArray array];
    NSInteger i = 0;
    for (NSString * title in titles) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat titleWidth = [title widthForFont:SYSTEMFONT(16)];
        btn.frame = CGRectMake(0, 0, titleWidth<35?35:titleWidth, 35);
        [btn setTitle:title forState:UIControlStateNormal];
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = SYSTEMFONT(16);
        [btn setTitleColor:kNavTintColor forState:UIControlStateNormal];
        btn.tag = [tags[i++] integerValue];
        //设置偏移btn
        if (isLeft) {
//            [btn setContentEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
        }else{
//            [btn setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, -10)];
        }

        UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
        [items addObject:item];
        [buttonArray addObject:btn];
    }
    if (isLeft) {
        self.navigationItem.leftBarButtonItems = items;
    } else {
        self.navigationItem.rightBarButtonItems = items;
    }
}

- (UIView*)customNavigationBar{
    if (!_customNavigationBar) {
        _customNavigationBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, self.navigationBarHidden?0.01:kTopHeight)];
        _customNavigationBar.backgroundColor = UIColor.whiteColor;
    }
    return _customNavigationBar;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    DLog(@"%@dealloc",NSStringFromClass([self class]));
}


#pragma mark -  屏幕旋转
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    //当前支持的旋转类型
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    // 是否支持旋转
    return NO;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    // 默认进去类型
    return   UIInterfaceOrientationPortrait;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
